﻿using System;
using System.Net;
using System.Net.Sockets;
using System.ComponentModel;
using FinosLibrary;
using System.Threading;
using System.Linq.Expressions;
using System.Windows;
namespace FinosLibrary.Client {
    public class ModelLogic:Logic,INotifyPropertyChanged {
        public ResourceDictionary resource { get { return Application.Current.Resources; } }
        public NetConnection connection { get { return NetConnection.getInstance(); } }
        private bool _isEnabled;
        public bool _IsEnabled {
            get { return !_isEnabled; }
        }
        public bool IsEnabled {
            get { return _isEnabled; }
            set { _isEnabled = value; this.NotifyPropertyChanged(x => x.IsEnabled); this.NotifyPropertyChanged(x => x._IsEnabled); }
        }
        [MessageMethod]
        public virtual void NetConnection_Connected() {
            IsEnabled = true;
        }
        [MessageMethod]
        public virtual void NetConnection_Disconnected() {
            IsEnabled = false;
        }
        public ModelLogic()
            : base() {
            IsEnabled = false;
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string propertyName) {
            syn.Post(_NotifyPropertyChanged, propertyName);
        }
        private void _NotifyPropertyChanged(object str) {
            if (PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(str as string));
            }
        }
        public void call(string fun, object b) {
            connection.SendPackage(new Package(fun, b));
        }
        public void call(string fun) {
            connection.SendPackage(new Package(fun));
        }
        public void callLocal(string fun,object b) {
            doLogic(fun, b);
        }
        public void callLocal(string fun) {
            doLogic(fun);
        }
    }
    public static class PropertyChangedBaseEx {
        public static void NotifyPropertyChanged<T, TProperty>(this　T propertyChangedBase, Expression<Func<T, TProperty>> expression) where T : ModelLogic {
            var memberExpression = expression.Body as MemberExpression;
            if (memberExpression != null) {
                string propertyName = memberExpression.Member.Name;
                propertyChangedBase.NotifyPropertyChanged(propertyName);
            } else
                throw new NotImplementedException();
        }
    }
}
